﻿using HandyControl.Controls;
using HandyControl.Data;
using System.Windows.Input;
using System.Windows.Interop;
using ETask.Client.Models;

namespace ETask.Client.ViewModels
{
    public class TaskViewModel:ViewModelBase
    {
        private string _content;

        public string Content
        {
            get => _content; 
            set => SetProperty(ref _content, value);
        }

        private DateTime _taskDate;
        public DateTime TaskDate
        {
            get => _taskDate;
            set
            {
                if (_taskDate != value)
                {
                    _taskDate = value;
                    OnPropertyChanged();
                }
            }
        }


        public ICommand AddCommand { get; }

        public ICommand CancelCommand { get; }

        public event EventHandler CloseRequested;

        public TaskViewModel()
        {
            this._taskDate = DateTime.Now;
            CancelCommand = new RelayCommand(CloseWindow);
            AddCommand = new RelayCommand(AddTask);
        }

        private void CloseWindow()
        {
            if (CloseRequested != null)
            {
                CloseRequested(this, EventArgs.Empty);
            }
        }

        private void AddTask()
        {
            if (string.IsNullOrEmpty(_content))
            {
                Growl.Error(new GrowlInfo()
                {
                    Message = "请输入任务内容",
                    WaitTime = 2,
                    IsCustom = true
                });
                return;
            }
            if (TaskDate == DateTime.MinValue)
            {
                Growl.Error(new GrowlInfo()
                {
                    Message = "任务完成时间有误",
                    WaitTime = 2,
                    IsCustom = true
                });
                return;
            }
            TaskItem taskItem = new TaskItem();
            taskItem.Content = Content;
            taskItem.TaskDate = TaskDate;
            TaskDataService.Instance.AddTask(taskItem);
            CloseWindow();
        }
    }
}
